﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;

namespace PPI.Data.Inspection
{
    public class DInspectionNote
    {
        public static DataSet SelectAllIdName(Int32 poleId, string connectionKey)
        {
            SqlDataAdapter da = null;
            try
            {
                da = new SqlDataAdapter("up_Inspections_Notes_SelectAllIdName", DBHelper.GetConnection(connectionKey));
                da.SelectCommand.CommandType = CommandType.StoredProcedure;
                DataSet ds = new DataSet();
                da.Fill(ds);
                return ds;
            }
            catch (Exception ex)
            {
                throw new Exception("Error executing up_Inspections_Notes_SelectAllIdName", ex);
            }
            finally
            {
                if (da != null) da.SelectCommand.Connection.Close();
            }
        }

        public static DataSet SelectAllIdName(string connectionKey)
        {
            SqlDataAdapter da = null;
            try
            {
                da = new SqlDataAdapter("up_Inspections_Notes_SelectAllIdName", DBHelper.GetConnection(connectionKey));
                da.SelectCommand.CommandType = CommandType.StoredProcedure;
                DataSet ds = new DataSet();
                da.Fill(ds);
                return ds;
            }
            catch (Exception ex)
            {
                throw new Exception("Error executing up_Inspections_Notes_SelectAllIdName", ex);
            }
            finally
            {
                if (da != null) da.SelectCommand.Connection.Close();
            }
        }

        public static DataSet SelectAll(Int32 resultFk,string connectionKey)
        {
            SqlDataAdapter da = null;
            try
            {
                da = new SqlDataAdapter("up_Inspections_ResultsXNotes_SelectAllByInspectionResult", DBHelper.GetConnection(connectionKey));
                da.SelectCommand.CommandType = CommandType.StoredProcedure;

                #region Result

                if(resultFk == Int32.MinValue)
                {
                    da.SelectCommand.Parameters.Add(
                        DBHelper.CreateIntParameter("@Vendor_InspVisit_XmissionStructures_ResultFk", ParameterDirection.Input, true, DBNull.Value));
                }else
                {
                    da.SelectCommand.Parameters.Add(
    DBHelper.CreateIntParameter("@Vendor_InspVisit_XmissionStructures_ResultFk", ParameterDirection.Input, false, resultFk));

                }

                #endregion

                DataSet ds = new DataSet();
                da.Fill(ds);
                return ds;
            }
            catch (Exception ex)
            {
                throw new Exception("Error executing up_Inspections_ResultsXNotes_SelectAllByInspectionResult", ex);
            }
            finally
            {
                if (da != null) da.SelectCommand.Connection.Close();
            }
        }

        public static void DeleteByInspectionResult(Int32 inspectionResultId, string connectionKey)
        {
            //up_Inspections_ResultsXNotes_DeleteByInspectionResult
            //@InspectionResultFk
            SqlDataAdapter da = null;
            try
            {
                da = new SqlDataAdapter("up_Inspections_ResultsXNotes_DeleteByInspectionResult", DBHelper.GetConnection(connectionKey));
                da.SelectCommand.CommandType = CommandType.StoredProcedure;
                da.SelectCommand.Parameters.Add(
                    DBHelper.CreateIntParameter("@Vendor_InspVisit_XmissionStructures_ResultFk", ParameterDirection.Input, false,
                                                inspectionResultId));
                da.SelectCommand.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                throw new Exception("Error executing up_Inspections_ResultsXNotes_DeleteByInspectionResult", ex);
            }
            finally
            {
                if (da != null) da.SelectCommand.Connection.Close();
            }
        }

        public static int Insert(Int32 noteFk, Int32 InspectionResult, string connectionKey)
        {
            SqlCommand cmd = null;
            SqlTransaction tr = null;
            try
            {
                cmd = new SqlCommand("up_Inspections_ResultsXNotes_Insert", DBHelper.GetConnection(connectionKey));
                tr = cmd.Connection.BeginTransaction();
                cmd.Transaction = tr;

                cmd.CommandType = CommandType.StoredProcedure;

                #region Note
                if (noteFk == Int32.MinValue)
                {
                    cmd.Parameters.Add(DBHelper.CreateIntParameter("@Vendor_InspVisit_XmissionStructures_NoteFk", ParameterDirection.Input, true, DBNull.Value));
                }
                else
                {
                    cmd.Parameters.Add(DBHelper.CreateIntParameter("@Vendor_InspVisit_XmissionStructures_NoteFk", ParameterDirection.Input, false, noteFk));
                }
                #endregion

                #region InspectionResult
                if (InspectionResult == Int32.MinValue)
                {
                    cmd.Parameters.Add(DBHelper.CreateIntParameter("@Vendor_InspVisit_XmissionStructures_ResultFk", ParameterDirection.Input, true, DBNull.Value));
                }
                else
                {
                    cmd.Parameters.Add(DBHelper.CreateIntParameter("@Vendor_InspVisit_XmissionStructures_ResultFk", ParameterDirection.Input, false, InspectionResult));
                }
                #endregion

                cmd.Parameters.Add(DBHelper.CreateIntParameter("@Vendor_InspVisit_XmissionStructures_ResultsXNoteId", ParameterDirection.ReturnValue, false, 0));
                cmd.ExecuteNonQuery();
                tr.Commit();
                return (Int32)cmd.Parameters["@Vendor_InspVisit_XmissionStructures_ResultsXNoteId"].Value;
            }
            catch (Exception ex)
            {
                if (tr != null)
                    tr.Rollback();
                return 0;
            }
            finally
            {
                if (cmd != null) cmd.Connection.Close();
            }
        }
    }
}
